package HelperFinancial.User;

import HelperFinancial.DataBase.AlertAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class AlertActivity extends Activity {
	private AlertActivity frame = this;
	private Spinner spinnerBalance;
	private Spinner spinnerDebits;
	private String balanceStatus;
	private String debitStatus;
	private int selectedBalance;
	private int selectedDebit;
	
	
	private AlertAdapter alertAdapter;

	/** Called when the activity is first create */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alert);
		
		alertAdapter = AlertAdapter.getInstance(frame);
		selectedBalance = 0;
		if(alertAdapter.getBalance().equalsIgnoreCase("sim")){
			selectedBalance =  0;
		}else{
			selectedBalance =  1;
		}
		selectedDebit = 0;
		if(alertAdapter.getDebits().equalsIgnoreCase("sim")){
			selectedDebit =  0;
		}else{
			selectedDebit =  1;
		}
		
		
//		dateEnd = (EditText) findViewById(R.id.camp_date_end_update);
		//dateInit = (EditText) findViewById(R.id.date_filter);
		Button save = (Button) findViewById(R.id.save_alert);
		save.setOnClickListener(onSave);
		
		spinnerBalance = (Spinner) findViewById(R.id.balance_alert_spinner);
        
        ArrayAdapter<String> adapterBalance = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, opcoes());  
        adapterBalance.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerBalance.setAdapter(adapterBalance);
        spinnerBalance.setSelection(selectedBalance);
        spinnerBalance.setOnItemSelectedListener(new OnItemSelectedListener() {  
        	 @Override  
        	 public void onItemSelected(AdapterView parent, View arg1,  
        	   int arg2, long arg3) {
        		 if(parent.getSelectedItem().toString().equalsIgnoreCase("ativado")){
        			 balanceStatus = "sim";
        		 }else{
        			 balanceStatus = "nao";
        		 }
        		 
        	 }  
        	 @Override  
        	 public void onNothingSelected(AdapterView parent) {
        		 if(selectedBalance == 0){
        			 balanceStatus = "sim";
        		 }else{
        			 balanceStatus = "nao";
        		 }
        	 }  
        });
        
		spinnerDebits = (Spinner) findViewById(R.id.debits_alert_spinner);
        
        ArrayAdapter<String> adapterViewDebit = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, opcoes());  
        adapterViewDebit.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerDebits.setAdapter(adapterViewDebit);
        spinnerDebits.setSelection(selectedDebit);
        spinnerDebits.setOnItemSelectedListener(new OnItemSelectedListener() {  
        	@Override  
       	 public void onItemSelected(AdapterView parent, View arg1,  
       	   int arg2, long arg3) {
       		 if(parent.getSelectedItem().toString().equalsIgnoreCase("ativado")){
       			 debitStatus = "sim";
       		 }else{
       			debitStatus = "nao";
       		 }
       		 
       	 }  
       	 @Override  
       	 public void onNothingSelected(AdapterView parent) {
       		 if(selectedDebit == 0){
       			debitStatus = "sim";
       		 }else{
       			debitStatus = "nao";
       		 }
       	 }  
        });

		
		
	}



	/**
	 * variavel privada que define o evento salvar.
	 */
	private View.OnClickListener onSave = new View.OnClickListener() {
		public void onClick(View v) {
			int id = alertAdapter.getId();
			alertAdapter.updateNegativeBalance(balanceStatus);
			alertAdapter.updateViewDebits(debitStatus);
			Toast.makeText(frame, "Salvo com sucesso!", Toast.LENGTH_LONG).show();
		}
	};

	
	public String[] opcoes(){
		String[] lista = new String[2];
		lista[0] = "Ativado";
		lista[1] = "Desativado";
		return lista;
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}
}